﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediaBrowser.Library.Entities;
using MediaBrowser.Library;
using MediaBrowser.Library.Extensions;

namespace MBWebService
{
    public static class MbMethods
    {
        public static void FindRecentUnwatchedChildren(Folder folder, SortedList<DateTime, BaseItem> foundNames, int maxSize, bool iterateThroughSeasons)
        {
            DateTime daysAgo = DateTime.Now.Subtract(DateTime.Now.Subtract(DateTime.Now.AddDays(-Plugin.KernelInstance.ConfigData.RecentItemDays)));
            if (folder != null && folder.Children.Any())
            {
                foreach (var item in folder.Children)
                {
                    // skip folders
                    if (item is Folder)
                    {
                        //don't return items inside protected folders
                        if (item.ParentalAllowed)
                        {
                            if (item is IContainer && !(item is Season))
                            {
                                //collapse series in the list
                                SortedList<DateTime, BaseItem> subItems = new SortedList<DateTime, BaseItem>();
                                FindRecentUnwatchedChildren(item as Folder, subItems, maxSize, iterateThroughSeasons);
                                if (item is Series)
                                {
                                    //we need to go another level into series to get actual items
                                    foreach (var seriesChild in (item as Folder).Children)
                                    {
                                        if (seriesChild is Season) FindRecentUnwatchedChildren(seriesChild as Folder, subItems, maxSize, iterateThroughSeasons);
                                    }
                                }
                                if (subItems.Count >= Plugin.KernelInstance.ConfigData.RecentItemCollapseThresh)
                                {
                                    //collapse into a series folder
                                    var ignore = item.BackdropImages; //force these to load so they will inherit
                                    DateTime createdTime = subItems.Keys.Max();

                                    while (foundNames.ContainsKey(createdTime))
                                    {
                                        createdTime = createdTime.AddMilliseconds(1);
                                    }
                                    foundNames.Add(createdTime, item);
                                }
                                else
                                {
                                    foreach (var pair in subItems)
                                    {
                                        foundNames.Add(pair.Key, pair.Value);
                                        if (foundNames.Count >= maxSize)
                                        {
                                            foundNames.RemoveAt(0);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (!(item is Series) || iterateThroughSeasons)
                                {
                                    //regular folder or box set - go into it
                                    (item as Folder).ValidateChildren(); //make sure we find the new stuff first
                                    SortedList<DateTime, BaseItem> subItems = new SortedList<DateTime, BaseItem>();
                                    FindRecentUnwatchedChildren(item as Folder, subItems, maxSize, iterateThroughSeasons);
                                    foreach (var pair in subItems)
                                    {
                                        foundNames.Add(pair.Key, pair.Value);
                                        if (foundNames.Count >= maxSize)
                                        {
                                            foundNames.RemoveAt(0);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else if (item is Video)
                    {
                        Video i = item as Video;
                        if (i.PlaybackStatus.WasPlayed == false && DateTime.Compare(item.DateCreated, daysAgo) > 0)
                        {
                            DateTime creationTime = item.DateCreated;
                            item.Parent = folder;
                            var ignore = item.Parent.BackdropImages; //force to load so they will inherit
                            ignore = item.BackdropImages;
                            while (foundNames.ContainsKey(creationTime))
                            {
                                creationTime = creationTime.AddMilliseconds(1);
                            }
                            foundNames.Add(creationTime, item);
                            if (foundNames.Count > maxSize)
                            {
                                foundNames.RemoveAt(0);
                            }
                        }
                    }
                }
            }
        }

        public static void FindRecentWatchedChildren(Folder folder, SortedList<DateTime, BaseItem> foundNames, int maxSize, bool iterateThroughSeasons)
        {
            DateTime daysAgo = DateTime.Now.Subtract(DateTime.Now.Subtract(DateTime.Now.AddDays(-Plugin.KernelInstance.ConfigData.RecentItemDays)));
            foreach (var item in folder.Children)
            {
                // recurse folders
                if (item is Folder)
                {
                    //don't return items inside protected folders
                    if (item.ParentalAllowed)
                    {
                        if (item is IContainer && !(item is Season))
                        {
                            //collapse series in the list
                            SortedList<DateTime, BaseItem> subItems = new SortedList<DateTime, BaseItem>();
                            FindRecentWatchedChildren(item as Folder, subItems, maxSize, iterateThroughSeasons);
                            if (item is Series)
                            {
                                //we need to go another level into series to get actual items
                                foreach (var seriesChild in (item as Folder).Children)
                                {
                                    if (seriesChild is Season) FindRecentWatchedChildren(seriesChild as Folder, subItems, maxSize, iterateThroughSeasons);
                                }
                            }
                            if (subItems.Count >= Plugin.KernelInstance.ConfigData.RecentItemCollapseThresh)
                            {
                                var ignore = item.BackdropImages; //force these to load so they will inherit
                                DateTime watchedTime = subItems.Keys.Max();
                                
                                while (foundNames.ContainsKey(watchedTime))
                                {
                                    // break ties 
                                    watchedTime = watchedTime.AddMilliseconds(1);
                                }
                                foundNames.Add(watchedTime, item);
                            }
                            else
                            {
                                foreach (var pair in subItems)
                                {
                                    var key = pair.Key;
                                    while (foundNames.ContainsKey(key))
                                    {
                                        // break ties 
                                        key = key.AddMilliseconds(1);
                                    }
                                    foundNames.Add(key, pair.Value);
                                    if (foundNames.Count >= maxSize)
                                    {
                                        foundNames.RemoveAt(0);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (!(item is Series) || iterateThroughSeasons)
                            {
                                //regular folder or box set - go into it
                                (item as Folder).ValidateChildren(); //make sure we find the new stuff first
                                SortedList<DateTime, BaseItem> subItems = new SortedList<DateTime, BaseItem>();
                                FindRecentWatchedChildren(item as Folder, subItems, maxSize, iterateThroughSeasons);
                                foreach (var pair in subItems)
                                {
                                    var key = pair.Key;
                                    while (foundNames.ContainsKey(key))
                                    {
                                        // break ties 
                                        key = key.AddMilliseconds(1);
                                    }
                                    foundNames.Add(key, pair.Value);
                                    if (foundNames.Count >= maxSize)
                                    {
                                        foundNames.RemoveAt(0);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (item is Video)
                    {
                        Video i = item as Video;
                        DateTime watchedTime = i.PlaybackStatus.LastPlayed;
                        if (i.PlaybackStatus.PlayCount > 0 && DateTime.Compare(watchedTime, daysAgo) > 0)
                        {
                            //only get ones watched within last 60 days
                            while (foundNames.ContainsKey(watchedTime))
                            {
                                // break ties 
                                watchedTime = watchedTime.AddMilliseconds(1);
                            }
                            item.Parent = folder;
                            var ignore = item.Parent.BackdropImages; //force these to load so they will inherit
                            ignore = item.BackdropImages;
                            foundNames.Add(watchedTime, item);
                            if (foundNames.Count > maxSize)
                            {
                                foundNames.RemoveAt(0);
                            }
                        }
                    }
                }
            }
        }
    }
}
